<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form toolbar">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">菜单名称:</label>
                    <div class="layui-input-inline">
                        <input name="authorityName" class="layui-input" placeholder="输入权限名称">
                    </div>
                </div>

                <div class="layui-inline"> 
                    <button class="layui-btn icon-btn" lay-filter="authoritiesTbSearch" lay-submit="">
                        <i class="layui-icon"></i>搜索
                    </button>&nbsp;
                </div>
            </div>
        </form>
        <!-- 表格操作列 -->
        <script type="text/html" id="authoritiesTbBar">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
        <!-- 数据表格 -->
        <table class="layui-table" id="menus-table" lay-filter="menus-table"></table>
        <!--表单弹窗-->
        <script type="text/html" id="authoritiesEditDialog">
            <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
                  style="padding-right: 20px;">
                <input name="authorityId" type="hidden"/>
                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">上级菜单</label>
                            <div class="layui-input-block">
                                <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label layui-form-required">权限名称:</label>
                            <div class="layui-input-block">
                                <input name="authorityName" placeholder="请输入权限名称" class="layui-input"
                                       lay-verType="tips" lay-verify="required" required/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label layui-form-required">权限类型:</label>
                            <div class="layui-input-block">
                                <input name="isMenu" type="radio" value="0" title="菜单" checked/>
                                <input name="isMenu" type="radio" value="1" title="资源"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单url:</label>
                            <div class="layui-input-block">
                                <input name="menuUrl" placeholder="请输入菜单url" class="layui-input"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">权限标识:</label>
                            <div class="layui-input-block">
                                <input name="authority" placeholder="请输入权限标识" class="layui-input"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单图标:</label>
                            <div class="layui-input-block">
                                <input name="menuIcon" placeholder="请输入菜单图标" class="layui-input"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单路径:</label>
                            <div class="layui-input-block">
                                <input name="menuPath" placeholder="请输入菜单路径" class="layui-input"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-inline" style="margin-left:40%">
                    <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
                    <button class="layui-btn layui-btn-primary layui-layer-close" type="button">取消</button>
                </div>
            </form>
        </script>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script src="../../layuiadmin/modules/common.js"></script>
<script>
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
        treeTable: 'treeTable',
        xmSelect: 'xmSelect'
    }).use('index','treeTable','xmSelect');

   layui.use(['layer', 'form', 'admin', 'treeTable', 'table', 'util', 'xmSelect'],function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var xmSelect = layui.xmSelect;
        var tbDataList = [];
        //进行渲染表格数据
        var insTb = treeTable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'authorityId',
            treePidName: 'parentId',
            elem:'#menus-table',
            url:'../../api/getAllTableMenus',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            tree: {
                iconIndex: 2,
                idName: 'authorityId',
                pidName: 'parentId',
                isPidData: true
            },
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'authorityName', title: '权限名称', minWidth: 150},
                {title: '菜单图标', templet: '<p><i class="layui-icon {{d.menuIcon}}"></i></p>', align: 'center', width: 100},
                {field: 'menuUrl', title: '菜单url'},
                {field: 'menuPath', title: '菜单路径', align: 'center'},
                {
                    title: '类型', templet: function (d) {
                        return [
                            '<span class="layui-badge layui-badge-green">菜单</span>',
                            '<span class="layui-badge layui-badge-gray">资源</span>'
                        ][d.isMenu];
                    }, align: 'center', width: 80
                },
                {title: '创建时间', templet: '<p>{{layui.util.toDateString(d.createTime)}}</p>', align: 'center'},
                {title: '更新时间', templet: '<p>{{layui.util.toDateString(d.updateTime)}}</p>', align: 'center'},
                {title: '操作', toolbar: '#authoritiesTbBar', align: 'center', width: 120}
            ]],
            done: function (data) {
                tbDataList = data;
            }
        });

       /* 表格操作列点击事件 */
       treeTable.on('tool(menus-table)', function (obj) {

           if (obj.event === 'edit') { // 修改
               console.log("数据流"+obj);
               showEditModel(obj.data);
           } else if (obj.event === 'del') { // 删除
               doDel(obj);
           }
       });

       /* 表格头工具栏点击事件 */
       treeTable.on('toolbar(menus-table)', function (obj) {
           if (obj.event === 'add') { // 添加
               showEditModel();
           } else if (obj.event === 'del') { // 删除
               var checkRows = insTb.checkStatus();
               if (checkRows.length === 0) {
                   layer.msg('请选择要删除的数据', {icon: 2});
                   return;
               }
               //console.log(checkRows);
               // 若勾选多个，通过for循环遍历删除
               for (let i = 1; i < checkRows.length; i++) {
                   var id = checkRows[i].authorityId;
                   console.log("获得到id为")
                   doDel({ids: id});
               }
           }
       });
       /* 表格搜索 */
       form.on('submit(authoritiesTbSearch)', function (data) {
           doTbSearch(data.field,"authorityId");
           return false;
       });

       /* 添加 */
       $('#authoritiesAddBtn').click(function () {
           showEditModel();
       });

       /* 显示表单弹窗 */
       function showEditModel(mData) {
           layer.open({
               type: 1,
               area: '600px',
               resize: false,//窗口大小不允许拖动
               title: (mData ? '修改' : '添加') + '权限',
               content: $('#authoritiesEditDialog').html(),
               success: function (layero, dIndex) {
                   // 回显表单数据
                   form.val('authoritiesEditForm', mData);
                   // 表单提交事件
                   form.on('submit(authoritiesEditSubmit)', function (data) {
                       data.field.parentId = insXmSel.getValue('valueStr');
                       var loadIndex = layer.load(2);

                       $.post(mData ? '../../api/updTableMenu' : '../../api/addTableMenu', data.field, function (res) {
                           layer.close(loadIndex);
                           if (res.code === 200) {
                               layer.close(dIndex);
                               layer.msg(res.msg, {icon: 1});
                               insTb.refresh();
                           } else {
                               layer.msg(res.msg, {icon: 2});
                           }
                       }, 'json');
                       //延时，保证数据提交后再刷新
                       sleep(800);
                       //父页面菜单刷新
                       reloadParentLeftNav();
                       return false;
                   });
                   // 渲染下拉树
                   var insXmSel = xmSelect.render({
                       el: '#authoritiesEditParentSel',
                       height: '250px',
                       data: insTb.options.data,
                       initValue: mData ? [mData.parentId] : [],
                       model: {label: {type: 'text'}},
                       prop: {
                           name: 'authorityName',
                           value: 'authorityId'
                       },
                       radio: true,
                       clickClose: true,
                       tree: {
                           show: true,
                           indent: 15,
                           strict: false,
                           expandedKeys: true
                       }
                   });
                   // 弹窗不出现滚动条
                   $(layero).children('.layui-layer-content').css('overflow', 'visible');
               }
           });

       }

       /* 删除 */
       function doDel(obj) {
           layer.confirm('确定要删除选中数据吗？', {
               skin: 'layui-layer-admin',
               shade: .1
           }, function (i) {
               layer.close(i);
               var loadIndex = layer.load(2);
               $.post('../../api/delTableMenuById', {id: obj.data ? obj.data.authorityId : obj.ids}, function (res) {
                   layer.close(loadIndex);
                   if (res.code === 200) {
                       layer.msg(res.msg, {icon: 1});
                       insTb.refresh();
                   } else {
                       layer.msg(res.msg, {icon: 2});
                   }
               }, 'json');
               //父页面菜单刷新
               reloadParentLeftNav();
           });
       }

       /* 搜索表格数据 */

       function doTbSearch(field, idName) {
           var ids = [], isClear = true;
           for (var i = 0; i < tbDataList.length; i++) {
               var item = tbDataList[i], flag = true;
               for (var f in field) {
                   if (!field.hasOwnProperty(f)) continue;
                   if (!field[f]) continue;
                   isClear = false;
                   if (!item[f] || item[f].indexOf(field[f]) === -1) {
                       flag = false;
                       break;
                   }
               }
               if (flag) ids.push(item[idName]);
           }
           if (isClear) {
               insTb.clearFilter();
           } else {
               insTb.filterData(ids);
           }
       }
   });
    /**
     * 刷新父页面的左侧菜单
     *
     */
    function reloadParentLeftNav() {
        window.parent.clearLeftNav();
        window.parent.load();
    }

    /**
     * 延时
     * @param time
     */
    var sleep = function(time) {
        var startTime = new Date().getTime() + parseInt(time, 10);
        while(new Date().getTime() < startTime) {}
    };
</script>


</body>
</html>


